﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ElectronicWMSA.Comm.Forms.Message
{
    public partial class WaitingBoxFrm : Form
    {

        #region Properties
        private int _MaxWaitTime;
        private int _WaitTime;
        private IAsyncResult _AsyncResult;
        private EventHandler<EventArgs> _Method;
        private bool _IsShown = true;
        private readonly int _EffectCount = 10;
        private readonly int _EffectTime = 500;
        private Timer _Timer;
        public string Message { get; private set; }
        public int TimeSpan { get; set; }
        public bool FormEffectEnable { get; set; }
        public WaitingBoxFrm(EventHandler<EventArgs> method, int maxWaitTime)
        {
            maxWaitTime *= 1000;
            Initialize(method, maxWaitTime);
        }
        private void Initialize(EventHandler<EventArgs> method, int maxWaitTime)
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterParent;
            this.ShowInTaskbar = false;
            _Timer = new Timer();
            _Timer.Interval = _EffectTime / _EffectCount;
            _Timer.Tick += _Timer_Tick;
            this.Opacity = 0;
            FormEffectEnable = true;
            TimeSpan = 500;
            Message = string.Empty;
            _MaxWaitTime = maxWaitTime;
            _WaitTime = 0;
            _Method = method;
            this.timer1.Interval = TimeSpan;
            this.timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            _WaitTime += TimeSpan;
            if (!this._AsyncResult.IsCompleted)
            {
                if (_WaitTime > _MaxWaitTime)
                {
                    Message = string.Format("处理数据超时{0}秒，结束当前操作！", _MaxWaitTime / 1000);
                    this.Close();
                }
            }
            else
            {
                this.Message = string.Empty;
                this.Close();
            }

        }

        private void WaitingBoxFrm_Shown(object sender, EventArgs e)
        {
            _AsyncResult = _Method.BeginInvoke(null, null, null, null);
            if (FormEffectEnable)
            {
                _Timer.Start();
            }
            else
                this.Opacity = 1;
        }

        private void WaitingBoxFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FormEffectEnable)
            {
                if (this.Opacity >= 1)
                    e.Cancel = true;
                _Timer.Start();
            }
        }

        private void _Timer_Tick(object sender, EventArgs e)
        {
            try
            {
                if (_IsShown)
                {
                    if (this.Opacity >= 1)
                    {
                        _Timer.Stop();
                        _IsShown = false;
                    }
                    this.Opacity += 1.00 / _EffectCount;
                }
                else
                {
                    if (this.Opacity <= 0)
                    {
                        _Timer.Stop();
                        _IsShown = true;
                        this.Close();
                    }
                    this.Opacity -= 1.00 / _EffectCount;
                }
            }
            catch (Exception)
            {
                this.Close();
            }
        }
        #endregion
    }
}
